import {createRouter, createWebHistory} from 'vue-router'
import Homepage from "../view/Homepage.vue"
import LayoutLoginafter from "../view/LayoutLoginafter.vue"
import LoginInfo from "../view/LoginInfo/index.vue"
import {useLoginStore} from "../stores/loginStore.js"
import {usemenuStore} from "../stores/menuStore.js"


export const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [

        {
            path: '/Homepage',
            name: 'Homepage',
            component: Homepage,
            meta: {
                requiresAuth: false
            },
        },
        {
            path: '/LayoutLoginafter',
            name: 'LayoutLoginafter',
            component: LayoutLoginafter,
            meta: {
                requiresAuth: true
            },
            children: [
                {
                    path: '/',
                    name: 'home',
                    component: () => import('../view/HomeView/index.vue'),
                    meta: {
                        requiresAuth: true,
                        key: "首页"
                    }

                },
                {
                    path: '/project',
                    name: 'project',
                    component: () => import('../view/ProjectInfo/index.vue'),
                    meta: {
                        requiresAuth: true,
                        key: "项目基础信息"
                    }
                },
                {
                    path: '/tunnel',
                    name: 'tunnel',
                    component: () => import('../view/TunnelInfo/index.vue'),
                    meta: {
                        requiresAuth: true,
                        key: "隧道设计信息"
                    }
                },
                {
                    path: '/build',
                    name: 'build',
                    component: () => import('../view/BuildManage/index.vue'),
                    meta: {
                        requiresAuth: true,
                        key: "施工监控检测"
                    }
                },
                {
                    path: '/geological',
                    name: 'geological',
                    component: () => import('../view/GeologicalInfo/index.vue'),
                    meta: {
                        requiresAuth: true,
                        key: "超前地质检测"
                    }
                },
                {
                    path: '/system',
                    name: 'system',
                    component: () => import('../view/SystemManage/index.vue'),
                    meta: {
                        requiresAuth: true,
                        key: "系统信息管理"
                    }
                },
                {
                    path: '/ucenter',
                    name: 'ucenter',
                    component: () => import('../view/UserCenter/index.vue'),
                    meta: {
                        requiresAuth: true,
                        key: "个人中心"
                    }
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: LoginInfo,
            meta: {
                requiresAuth: false
            }

        },
        {
            path: '/:pathMatch(.*)*',
            name: 'notfound',
            component: () => import('../view/Notfound/index.vue'),
            meta: {
                requiresAuth: false
            }

        },


    ],
});


router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth) {
        const loginStore = useLoginStore()
        if (!loginStore.token) {
            next({
                path: "/Homepage"
            })
        } else {
            next()
        }
    } else {
        next()
    }
})

router.afterEach((to, from) => {
    console.log(to)
    localStorage.setItem("active", to.path)

    if (to.meta.key) {
        const menuStore = usemenuStore()
        menuStore.breadcrumb = to.meta.key
    }


})


export default router
